﻿using System.Collections.Generic;

using OnlineFamilyCalendar.UI.Presentation.Views;
using OnlineFamilyCalendar.Common.Security;
using OnlineFamilyCalendar.Service;
using OnlineFamilyCalendar.UI.Web.AuthenticationModule;

namespace OnlineFamilyCalendar.UI.Presentation
{
    public class LoginPresenter
    {
        private ILoginView _view;
        private IFamilyMemberService _familyMemberService;

        public LoginPresenter(ILoginView view)
        {
            _view = view;
        }

        public void Login()
        {
            _familyMemberService = new FamilyMemberService();
            OnlineFamilyCalendarIdentity userIdentity = _familyMemberService.AuthenticateFamilyMember(_view.Loginname, _view.Password);

            if (!userIdentity.IsAuthenticated)
            {
                return;
                // ToDo throw an exception
            }

            OnlineFamilyCalendarPrincipal principal = new OnlineFamilyCalendarPrincipal(userIdentity, userIdentity.Role);
            _view.UserPrincipal = principal;

            Authentication.RedirectFromLoginPage(userIdentity);               
        }
    }
}
